memset関数は、メモリ領域に同じ値を指定された長さだけ設定します。
#include <string.h>
void *memset(void *s, int c, size_t n);
*sはメモリ領域の先頭アドレスを指定します。
cは設定する値を指定します。
nは長さをバイト単位で指定します。
戻り値として、第1引数sのアドレスを返します。
プログラム 例
#include <stdio.h>
#include <string.h>
int main()
{
/* 文字の埋め込み関数 */
char *RepeatChar(char *buff, int c, int n);
char buff[11];
int line_cnt;
const int line_max = 10;
int col;
for (line_cnt = 1, col = line_max;
line_cnt <= line_max; ++line_cnt, ++col) {
printf('%s', RepeatChar(buff, ' ', line_max - line_cnt));
printf('%s\n', RepeatChar(buff, '*', col - (line_max - line_cnt)));
}
return 0;
}
/* 文字の埋め込み */
char *RepeatChar(char *p_buff, int p_c, int p_n)
{
memset(p_buff, p_c, p_n);
*(p_buff + p_n) = '\0';
return p_buff;
}
例の実行結果
$ ./memset.exe
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
$